#include "Texture.h"
#include "TextureIdPool.h"

namespace core
{
	Texture::Texture(int width, int height, GLvoid* imageData)
	{
		_glId = TextureIdPool::Take();

		_width = width;
		_height = height;

		glBindTexture(GL_TEXTURE_2D, _glId);   // 2d texture (x and y size)
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // scale linearly when image bigger than texture
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); // scale linearly + mipmap when image smalled than texture
		glTexImage2D(GL_TEXTURE_2D, 0, 4, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
		gluBuild2DMipmaps(GL_TEXTURE_2D, 4, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
	}

	Texture::~Texture()
	{
		TextureIdPool::Put(_glId);
	}
}
